home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Applications… / All Shapes with Printing ƒ / printing.c < prev   
Encoding:
C/C++ Source or Header  |  1995-04-10  |  8.1 KB  |  309 lines  |  [TEXT/KAHL]

  1. /*
  2.     Printing.c
  3.     
  4.     This file contains the printing routines for "all shapes".
  5.  
  6.     ©1992-1994  Apple Computer, Inc.
  7.     All rights reserved.
  8.     
  9.     3/22/94 - dmh - Updated for B4.
  10.     3/24/94 - dmh - general cleanup/debugging.
  11.     5/3/94    - dmh - more cleanup/debugging.
  12.     8/24/94 - dmh - universalized.
  13. */
  14.  
  15. #include "PrintingManager.h"
  16. #include "GXExceptions.h"
  17. #include "graphics shell.h"
  18. #include "memory.h"
  19.  
  20.  
  21. /*------ SetUpEditMenuRec ------------------------------------------------------------------------------------*/
  22. //
  23. //    This routine sets up an gxEditMenuRecord which references our edit menu.  This structure
  24. //    is used by the GXJobDefaultFormatDialog and GXJobPrintDialog calls to allow cut, copy, & paste
  25. //    operations from the dialogs.
  26. //
  27. void SetUpEditMenuRec(gxEditMenuRecord *edMenuRec)
  28. {
  29.  
  30.     edMenuRec->editMenuID = mEdit;
  31.     edMenuRec->cutItem = iCut;
  32.     edMenuRec->copyItem = iCopy;
  33.     edMenuRec->pasteItem = iPaste;
  34.     edMenuRec->clearItem = iClear;
  35.     edMenuRec->undoItem = iUndo;
  36. }
  37.  
  38.  
  39. /*------ DoFormat ------------------------------------------------------------------------------------*/
  40. //
  41. //    This routine performs GX's equivalent of the Page Setup (PrStlDialog) call of
  42. //    the old printing architecture.
  43. //
  44. OSErr DoFormat(WindowPtr wind, gxDialogResult    *result)
  45. {
  46.     OSErr                err;
  47.     gxEditMenuRecord    edMenuRec;
  48.     gxJob                docJob;
  49.  
  50. //    If we have a non-nil WindowPtr, set up our edit menu record and handle the job
  51. //    format dialog.  Remember to check for errors.
  52.  
  53.     if (wind)
  54.     {
  55.         docJob = GetDocJob(wind);
  56.         SetUpEditMenuRec(&edMenuRec);
  57.         *result = GXJobDefaultFormatDialog(docJob, &edMenuRec);
  58.     }
  59.     
  60.     return GXGetJobError(docJob);
  61. }
  62.  
  63.  
  64. /*------ DoPrinting ----------------------------------------------------------------------------------*/
  65. //
  66. //    This routine performs GX's equivalent of the Print gxJob Dialog (PrJobDialog) call of
  67. //    the old printing architecture, and then prints the document if the user wants to.
  68. //
  69. OSErr DoPrinting(WindowPtr wind)
  70. {
  71.     OSErr            err = noErr;
  72.     gxDialogResult    result;
  73.     gxEditMenuRecord    edMenuRec;
  74.     gxJob                docJob;
  75.  
  76. //    If we have a non-nil WindowPtr, set up our edit menu record and handle the print
  77. //    job dialog.  Remember to check for errors.  If no errors occur, and the user clicks
  78. //  ok, print the window's document.
  79.  
  80.     if (wind)
  81.     {
  82.         docJob = GetDocJob(wind);
  83.         
  84.         SetUpEditMenuRec(&edMenuRec);
  85.         result = GXJobPrintDialog(docJob, &edMenuRec);
  86.         err = GXGetJobError(docJob);
  87.                 
  88.         if ((result == gxOKSelected) && !(err))
  89.             err = DoPrintLoop(wind);
  90.     }
  91.     
  92.     return err;
  93. }
  94.  
  95.  
  96.  
  97. /*------ DoPrintLoop ----------------------------------------------------------------------------------*/
  98. //
  99. //    This routine prints one copy of the window's document using whatever job and format
  100. //    is currently attached to it.  (If the window wwere just created and then the user
  101. //    selected Print One Copy w/o first selecting Page Setup…, the system default job and
  102. //    format are stored with this document.
  103. //
  104. OSErr DoPrintLoop(WindowPtr wind)
  105. {
  106.     OSErr        err = noErr;
  107.     gxJob            docJob;
  108.     Str255        title;
  109.  
  110. //    If we have a non-nil WindowPtr, start the print job, print a page and then finish
  111. //    the job.  Since we have just a one page document, we don't bother counting pages.
  112. //    Remember to check those errors!
  113.  
  114.     if (wind)
  115.     {
  116.         docJob = GetDocJob(wind);
  117.         GetWTitle(wind, title);
  118.     
  119.         GXStartJob(docJob, title, 1);
  120.         err = GXGetJobError(docJob);
  121.         
  122.         if (!err)
  123.         {
  124.               GXPrintPage(docJob, 1, GXGetJobFormat(docJob, 1), GetDocShape(wind));
  125.             err = GXGetJobError(docJob);
  126.         }
  127.         
  128.         GXFinishJob(docJob);
  129.         if (!err) err = GXGetJobError(docJob);
  130.     }
  131.     return err;
  132. }
  133.  
  134.  
  135. /*******************************************************************
  136.     DoPrintOneCopy sets up our job collection items for printing
  137.     one copy of a document, and then prints the document.
  138.     
  139. ********************************************************************/
  140.  
  141. OSErr DoPrintOneCopy(WindowPtr wind)
  142. {
  143.     OSErr                    err;
  144.     Collection                jobCollection;
  145.     gxCopiesInfo            copiesInfo;
  146.     gxFileDestinationInfo    destInfo;
  147.     gxPageRangeInfo            pageRangeInfo;
  148.     Ptr                        oldCopiesInfo = nil, oldPageRangeInfo = nil, oldDestInfo = nil;
  149.     long                    oldCopiesSize, oldPageRangeInfoSize, oldDestInfoSize;
  150.     TH_Doc                    doc = (TH_Doc) GetWRefCon(wind);
  151.  
  152. /* Get the job collection and set it up to print one copy…    */
  153.  
  154.     jobCollection = GXGetJobCollection(GetDocJob(wind));
  155.  
  156.  
  157. /* Set number of copies to 1.            */
  158.     
  159.     copiesInfo.copies = 1;
  160.     err = MyReplaceCollectionItem(&copiesInfo, sizeof(gxCopiesInfo),
  161.                                     gxCopiesTag, gxPrintingTagID,
  162.                                     jobCollection, &oldCopiesInfo, &oldCopiesSize);
  163.     nrequire(err, ReplaceCopies_error);
  164.  
  165.  
  166. /* Set page range to "all".    */
  167.     
  168.     pageRangeInfo.simpleRange.optionChosen = gxDefaultPageRange;
  169.     pageRangeInfo.minFromPage = 1;
  170.     pageRangeInfo.simpleRange.fromPage = 1;        // This app only handles one page docs.
  171.     pageRangeInfo.maxToPage = 1;
  172.     pageRangeInfo.simpleRange.toPage = 1;
  173.     pageRangeInfo.simpleRange.printAll = true;
  174.     err = MyReplaceCollectionItem(&pageRangeInfo, sizeof(gxPageRangeInfo),
  175.                                     gxPageRangeTag, gxPrintingTagID,
  176.                                     jobCollection, &oldPageRangeInfo, &oldPageRangeInfoSize);
  177.     nrequire(err, ReplacePageRange_error);
  178.  
  179.  
  180. /* Set destination to "printer".        */
  181.  
  182.     destInfo.toFile = false;
  183.     err = MyReplaceCollectionItem(&destInfo, sizeof(gxFileDestinationInfo),
  184.                                   gxFileDestinationTag, gxPrintingTagID,
  185.                                   jobCollection, &oldDestInfo, &oldDestInfoSize);
  186.     nrequire(err, ReplaceDestination_error);
  187.  
  188.  
  189. /* Print one copy of our document.        */
  190.  
  191.     err = DoPrintLoop(wind);
  192.  
  193.  
  194. /*    Restore original number of copies, page range, and output
  195.     destination in case anybody uses that info. */
  196.  
  197. ReplaceCopies_error:
  198.     MyReplaceCollectionItem(oldCopiesInfo, oldCopiesSize,
  199.                               gxCopiesTag, gxPrintingTagID,
  200.                             jobCollection, nil, nil);
  201.  
  202. ReplacePageRange_error:
  203.     MyReplaceCollectionItem(oldPageRangeInfo, oldPageRangeInfoSize,
  204.                             gxPageRangeTag, gxPrintingTagID,
  205.                             jobCollection, nil, nil);
  206.  
  207. ReplaceDestination_error:
  208.     MyReplaceCollectionItem(oldDestInfo, oldDestInfoSize,
  209.                             gxFileDestinationTag, gxPrintingTagID,
  210.                             jobCollection, nil, nil);
  211.  
  212.  
  213. /* Dispose of the pointers that MyReplaceCollectionItem created. */
  214.  
  215.     if (oldCopiesInfo)
  216.         DisposePtr(oldCopiesInfo);
  217.  
  218.     if (oldPageRangeInfo)
  219.         DisposePtr(oldPageRangeInfo);
  220.  
  221.     if (oldDestInfo)
  222.         DisposePtr(oldDestInfo);
  223.  
  224.     return err;
  225. }
  226.  
  227.  
  228.  
  229. /*******************************************************************
  230.     MyReplaceCollectionItem is a generic routine that replaces a
  231.     collection item with the passed data.  If the oldData parameter
  232.     is not nil, this routine creates a pointer and returns the data
  233.     that's being replaced in it.  (If the item doesn't already exist,
  234.     then a copy of the newData is returned instead.)
  235.     
  236. ********************************************************************/
  237.  
  238. OSErr MyReplaceCollectionItem(void *newData, long collectSize,
  239.                               OSType collectType, long collectID,
  240.                               Collection whichCollection,
  241.                               Ptr *oldData, long *oldDataSize)
  242. {
  243.     OSErr    err = noErr;
  244.     long    index;
  245.  
  246. /*
  247.     If we're supposed to return the old data, get it.
  248.     If there is no old data, return a copy of the new data.
  249. */
  250.  
  251.     if (oldData)
  252.     {
  253.         err = GetCollectionItemInfo(whichCollection,
  254.                                     collectType,
  255.                                     collectID,
  256.                                     dontWantIndex,
  257.                                     oldDataSize,
  258.                                     dontWantAttributes);
  259.  
  260.         if (err)
  261.         {
  262.             *oldDataSize = collectSize;
  263.             *oldData = NewPtrSys(*oldDataSize);
  264.             if (!(err = MemError()))
  265.                 BlockMove(newData, *oldData, collectSize);
  266.         }
  267.         else
  268.         {
  269.             *oldData = NewPtrSys(*oldDataSize);
  270.             if (!(err = MemError()))
  271.                 err = GetCollectionItem(whichCollection,
  272.                                         collectType,
  273.                                         collectID,
  274.                                         dontWantSize,
  275.                                         *oldData);
  276.         }
  277.     }
  278.  
  279.     nrequire(err, CouldNotSetOldData);
  280.  
  281.  
  282. // If we're adding a new collection item, do so.  Otherwise, get the
  283. // existing item's index and replace the old collection item.
  284.  
  285.     err = AddCollectionItem(whichCollection,
  286.                             collectType,
  287.                             collectID,
  288.                             collectSize,
  289.                             newData);
  290.  
  291.     if (err == collectionItemLockedErr)
  292.     {
  293.         err = GetCollectionItemInfo(whichCollection,
  294.                                     collectType,
  295.                                     collectID,
  296.                                     &index,
  297.                                     dontWantSize,
  298.                                     dontWantAttributes);
  299.         if (!err)
  300.             err = ReplaceIndexedCollectionItem(whichCollection,
  301.                                                index,
  302.                                                collectSize,
  303.                                                newData);
  304.     }
  305.  
  306. CouldNotSetOldData:
  307.     return err;
  308. }
  309.